#!/usr/bin/env python3
# HFT系统工具链集成脚本

import argparse
import subprocess
import sys
import os
from pathlib import Path

class HFTToolkit:
    TOOLS = {
        'deploy': './scripts/deploy.sh',
        'monitor': './scripts/monitor.py',
        'config': './scripts/config_manager.py',
        'analyze': './scripts/performance_analyzer.py'
    }
    
    def __init__(self):
        self.script_dir = Path(__file__).parent.absolute()
        
    def run_tool(self, tool_name, args):
        """运行指定工具"""
        tool_path = self.TOOLS.get(tool_name)
        if not tool_path:
            print(f"错误: 未知工具 '{tool_name}'")
            return False
            
        full_path = os.path.join(self.script_dir, tool_path)
        
        if not os.path.exists(full_path):
            print(f"错误: 工具不存在 '{full_path}'")
            return False
            
        try:
            if tool_path.endswith('.py'):
                cmd = [sys.executable, full_path] + args
            else:
                cmd = [full_path] + args
                
            subprocess.run(cmd, check=True)
            return True
        except subprocess.CalledProcessError as e:
            print(f"工具执行失败: {e}")
            return False
        except Exception as e:
            print(f"错误: {e}")
            return False
    
    def list_tools(self):
        """列出所有可用工具"""
        print("可用工具:")
        for name, path in self.TOOLS.items():
            print(f"  {name.ljust(10)} - {path}")

def main():
    parser = argparse.ArgumentParser(description='HFT系统工具链集成')
    subparsers = parser.add_subparsers(dest='command', required=True)
    
    # 部署命令
    deploy_parser = subparsers.add_parser('deploy', help='运行部署工具')
    deploy_parser.add_argument('args', nargs=argparse.REMAINDER, help='部署参数')
    
    # 监控命令
    monitor_parser = subparsers.add_parser('monitor', help='运行监控工具')
    monitor_parser.add_argument('args', nargs=argparse.REMAINDER, help='监控参数')
    
    # 配置命令
    config_parser = subparsers.add_parser('config', help='运行配置管理工具')
    config_parser.add_argument('args', nargs=argparse.REMAINDER, help='配置参数')
    
    # 分析命令
    analyze_parser = subparsers.add_parser('analyze', help='运行性能分析工具')
    analyze_parser.add_argument('args', nargs=argparse.REMAINDER, help='分析参数')
    
    # 列表命令
    subparsers.add_parser('list', help='列出所有可用工具')
    
    args = parser.parse_args()
    toolkit = HFTToolkit()
    
    if args.command == 'list':
        toolkit.list_tools()
    else:
        success = toolkit.run_tool(args.command, args.args)
        sys.exit(0 if success else 1)

if __name__ == '__main__':
    main()